<template>
    <div class="container">
        <el-form :model="ruleForm2" status-icon :rules="rules2" ref="ruleForm2" label-width="100px"
                 class="demo-ruleForm">
            <el-form-item label="项目名称" prop="p1">
                <el-input v-model="ruleForm2.p1" auto-complete="off"></el-input>
            </el-form-item>
            <el-form-item label="项目简介" prop="p2">
                <el-input type="textarea" v-model="ruleForm2.p2" auto-complete="off"></el-input>
            </el-form-item>
            <el-form-item label="项目开始时间" prop="p3">
                <template>
                    <div class="block">
                        <el-date-picker
                            v-model="ruleForm2.p3"
                            type="datetime"
                            placeholder="选择日期"
                            value-format="yyyy-MM-dd HH:mm:ss">
                        </el-date-picker>
                    </div>
                </template>
            </el-form-item>
            <el-form-item label="项目截止时间" prop="p4">
                <template>
                    <div class="block">
                        <el-date-picker
                            v-model="ruleForm2.p4"
                            type="datetime"
                            placeholder="选择日期"
                            value-format="yyyy-MM-dd HH:mm:ss">
                        </el-date-picker>
                    </div>
                </template>
            </el-form-item>
            <br>
            <el-form-item>
                <el-button type="primary" @click="submitForm('ruleForm2')">提交</el-button>
            </el-form-item>
        </el-form>
    </div>
</template>

<script>
    import {quillEditor} from 'vue-quill-editor';
    import pub from '../../utils/pub'
    import upload from '@/components/common/Upload'

    export default {
        components: {
            quillEditor,
            upload,
        },
        data: function () {
            return {
                roleList: [],
                isHasManager: false,
                admin: {},
                defaultProps: {
                    children: 'children',
                    label: 'name'
                },
                ruleForm2: {
                    p1: '', // 项目名称
                    p2: '', // 项目简介
                    p3: '', // 项目开始时间
                    p4: '', // 项目结束时间
                },
                rules2: {
                    p1: [
                        {required: true, message: '请输入项目名称', trigger: 'blur'}
                    ],
                    p2: [
                        {required: true, message: '请输入项目简介', trigger: 'blur'}
                    ],
                    p3: [
                        {required: true, message: '请选择项目开始时间', trigger: 'blur'}
                    ],
                    p4: [
                        {required: true, message: '请选择项目截止时间', trigger: 'blur'}
                    ],
                }
            }
        },
        props: ['uid'],
        methods: {
            loadopen() {
                this.loading = this.$loading({
                    lock: true,
                    text: 'Loading',
                    spinner: 'el-icon-loading',
                    background: 'rgba(0, 0, 0, 0.2)'
                });
                setTimeout(() => {this.loading.close();}, 5000);
            },
            loadclose() {
                this.loading.close();
            },
            // 获取角色信息
            getInfo() {
                this.clear();
                if(!this.uid) {
                    this.ruleForm2.p1 = '';
                    this.ruleForm2.p2 = '';
                    this.ruleForm2.p3 = '';
                    this.ruleForm2.p4 = '';
                    return;
                }
                this.loadopen();
                this.config.get('/housinggestate/project/project/findById', {id: this.uid}).then(res => {
                    this.loadclose();
                    if (res.data.code === '0') {
                        // console.log(res.data, '获取角色信息');
                        this.project = res.data.project;
                        this.ruleForm2.id = this.project.id;
                        this.ruleForm2.p1 = this.project.name;
                        this.ruleForm2.p2 = this.project.synopsis;
                        this.ruleForm2.p3 = this.project.start_time;
                        this.ruleForm2.p4 = this.project.end_time;
                    } else {
                        this.$message.error(this.config.handleError(res.data));
                    }
                });
            },
            // 清空表单
            clear(){
                this.ruleForm2.p1 = '';
                this.ruleForm2.p2 = '';
                this.ruleForm2.p3 = '';
                this.ruleForm2.p4 = '';
            },
            submitForm(formName) {
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        let obj = {
                            'p.id': this.uid,
                            'p.name': this.ruleForm2.p1,
                            'p.synopsis': this.ruleForm2.p2,
                            'p.start_time': this.ruleForm2.p3,
                            'p.end_time': this.ruleForm2.p4,
                        };
                        // console.log(obj);
                        let url = this.uid ? '/housinggestate/project/project/update' : '/housinggestate/project/project/add';
                        this.loadopen();
                        this.config.post(url, obj).then(res => {
                            this.loadclose();
                            if (res.data.code === '0') {
                                this.$message({
                                    message: res.data.message,
                                    type: 'success'
                                });
                            } else {
                                this.$message.error(this.config.handleError(res.data));
                            }
                            this.$emit('done', '');
                        });
                    } else {
                        console.log('error submit!!');
                        return false;
                    }
                });
            },
        },
        mounted() {
        }
    }
</script>
